<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 页面标题 -->
				<view class="page-header">
					<view class="back-button" @tap="goBack">
						<text class="back-icon">←</text>
					</view>
					<view class="header-info">
						<text class="tool-icon">💕</text>
						<text class="tool-title">缘分配对</text>
						<text class="tool-subtitle">生辰八字配对分析</text>
					</view>
				</view>
				
				<!-- 参数输入区域 -->
				<app-card>
					<view class="input-section">
						<text class="section-title">男方信息</text>
						
						<view class="form-item">
							<text class="form-label">姓名</text>
							<input 
								v-model="params.male.name" 
								placeholder="请输入男方姓名"
								class="input-field"
							/>
						</view>
						
						<view class="form-item">
							<text class="form-label">生辰八字</text>
							<picker 
								mode="date" 
								:value="params.male.birthday" 
								@change="onMaleBirthdayChange"
								class="birthday-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayMaleBirthday }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">出生时辰</text>
							<picker 
								mode="time" 
								:value="params.male.time" 
								@change="onMaleTimeChange"
								class="time-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayMaleTime }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
					</view>
				</app-card>
				
				<app-card>
					<view class="input-section">
						<text class="section-title">女方信息</text>
						
						<view class="form-item">
							<text class="form-label">姓名</text>
							<input 
								v-model="params.female.name" 
								placeholder="请输入女方姓名"
								class="input-field"
							/>
						</view>
						
						<view class="form-item">
							<text class="form-label">生辰八字</text>
							<picker 
								mode="date" 
								:value="params.female.birthday" 
								@change="onFemaleBirthdayChange"
								class="birthday-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayFemaleBirthday }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">出生时辰</text>
							<picker 
								mode="time" 
								:value="params.female.time" 
								@change="onFemaleTimeChange"
								class="time-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayFemaleTime }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
					</view>
				</app-card>
				
				<!-- 查询按钮 -->
				<view class="action-section">
					<app-button 
						:text="loading ? '分析中...' : '开始分析'" 
						type="primary"
						@tap="queryCompatibility"
						:disabled="!canQuery || loading"
						:loading="loading"
					></app-button>
				</view>
				
				<!-- 结果显示区域 -->
				<app-card v-if="compatibilityResult">
					<view class="result-section">
						<text class="section-title">配对分析</text>
						
						<view class="compatibility-analysis">
							<view class="overall-compatibility">
								<view class="compatibility-score">
									<view class="score-circle">
										<text class="score-text">{{ compatibilityResult.overall.score }}</text>
										<text class="score-unit">分</text>
									</view>
									<text class="score-level">{{ compatibilityResult.overall.level }}</text>
								</view>
								<view class="compatibility-desc">
									<text class="desc-text">{{ compatibilityResult.overall.description }}</text>
								</view>
							</view>
							
							<view class="compatibility-details">
								<view 
									v-for="(detail, index) in compatibilityResult.details" 
									:key="index"
									class="detail-item"
								>
									<text class="detail-aspect">{{ detail.aspect }}</text>
									<view class="detail-content">
										<text class="detail-desc">{{ detail.description }}</text>
										<view class="detail-score">
											<text class="score-text">{{ detail.score }}分</text>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 建议 -->
				<app-card v-if="compatibilityResult && compatibilityResult.suggestions">
					<view class="suggestions-section">
						<text class="section-title">感情建议</text>
						
						<view class="suggestions-list">
							<view 
								v-for="(suggestion, index) in compatibilityResult.suggestions" 
								:key="index"
								class="suggestion-item"
							>
								<view class="suggestion-icon">
									<text class="icon-text">💡</text>
								</view>
								<text class="suggestion-text">{{ suggestion }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 幸运元素 -->
				<app-card v-if="compatibilityResult && compatibilityResult.luckyElements">
					<view class="lucky-section">
						<text class="section-title">幸运元素</text>
						
						<view class="lucky-elements">
							<view class="element-item">
								<text class="element-label">幸运色</text>
								<text class="element-value">{{ compatibilityResult.luckyElements.color }}</text>
							</view>
							<view class="element-item">
								<text class="element-label">幸运数字</text>
								<text class="element-value">{{ compatibilityResult.luckyElements.number }}</text>
							</view>
							<view class="element-item">
								<text class="element-label">吉利日期</text>
								<text class="element-value">{{ compatibilityResult.luckyElements.date }}</text>
							</view>
							<view class="element-item">
								<text class="element-label">吉利方位</text>
								<text class="element-value">{{ compatibilityResult.luckyElements.direction }}</text>
							</view>
						</view>
					</view>
				</app-card>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import ToolsService from '@/utils/tools-service.js'
import UserStateManager from '@/utils/user-state-manager.js'

export default {
	name: 'CompatibilityToolPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			params: {
				male: {
					name: '',
					birthday: '',
					time: ''
				},
				female: {
					name: '',
					birthday: '',
					time: ''
				}
			},
			compatibilityResult: null,
			loading: false,
			toolsService: null
		}
	},
	computed: {
		canQuery() {
			return this.params.male.name.trim().length > 0 && 
				   this.params.male.birthday && 
				   this.params.female.name.trim().length > 0 && 
				   this.params.female.birthday
		},
		
		displayMaleBirthday() {
			return this.params.male.birthday || '选择生日'
		},
		
		displayMaleTime() {
			return this.params.male.time || '选择时辰'
		},
		
		displayFemaleBirthday() {
			return this.params.female.birthday || '选择生日'
		},
		
		displayFemaleTime() {
			return this.params.female.time || '选择时辰'
		}
	},
	onLoad() {
		this.initData()
	},
	methods: {
		initData() {
			this.toolsService = new ToolsService()
				
			// 自动填写用户信息
			this.autoFillUserInfo()
		},
			
		/**
		 * 自动填写用户信息
		 */
		autoFillUserInfo() {
			const autoFillInfo = UserStateManager.getAutoFillInfo()
			if (autoFillInfo) {
				// 填写男方信息（使用已登录用户信息）
				if (autoFillInfo.gender === 'male') {
					this.params.male.name = autoFillInfo.name
					this.params.male.birthday = autoFillInfo.birthday
				} else if (autoFillInfo.gender === 'female') {
					// 如果用户是女性，则填入女方信息
					this.params.female.name = autoFillInfo.name
					this.params.female.birthday = autoFillInfo.birthday
				}
					
				// 显示提示
				uni.showToast({
					title: '已自动填入个人信息',
					icon: 'none',
					duration: 2000
				})
			} else {
				// 提示用户登录或完善信息
				setTimeout(() => {
					UserStateManager.promptCompleteInfo()
				}, 1000)
			}
		},
		
		goBack() {
			uni.navigateBack()
		},
		
		onMaleBirthdayChange(e) {
			this.params.male.birthday = e.detail.value
		},
		
		onMaleTimeChange(e) {
			this.params.male.time = e.detail.value
		},
		
		onFemaleBirthdayChange(e) {
			this.params.female.birthday = e.detail.value
		},
		
		onFemaleTimeChange(e) {
			this.params.female.time = e.detail.value
		},
		
		async queryCompatibility() {
			if (!this.canQuery) {
				uni.showToast({
					title: '请完善双方信息',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			
			try {
				const result = await this.toolsService.getCompatibilityInfo(this.params)
				
				if (result.success) {
					this.compatibilityResult = result.data
					uni.showToast({
						title: '分析完成',
						icon: 'success'
					})
				} else {
					throw new Error(result.message || '分析失败')
				}
			} catch (error) {
				console.error('缘分配对分析失败:', error)
				uni.showToast({
					title: '分析失败，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.page-header {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;
	
	.back-button {
		width: 80rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(255, 255, 255, 0.9);
		border-radius: 40rpx;
		margin-right: 24rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
		
		.back-icon {
			font-size: 32rpx;
			color: #000000;
			font-weight: bold;
		}
	}
	
	.header-info {
		flex: 1;
		
		.tool-icon {
			font-size: 64rpx;
			margin-bottom: 8rpx;
		}
		
		.tool-title {
			display: block;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
			margin-bottom: 8rpx;
		}
		
		.tool-subtitle {
			display: block;
			font-size: 26rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.input-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.form-item {
		margin-bottom: 32rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.form-label {
			display: block;
			font-size: 28rpx;
			color: #000000;
			margin-bottom: 16rpx;
		}
		
		.input-field {
			width: 100%;
			height: 88rpx;
			padding: 0 24rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
		}
		
		.birthday-picker,
		.time-picker {
			.picker-display {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 88rpx;
				padding: 0 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				
				.picker-text {
					font-size: 28rpx;
					color: #000000;
				}
				
				.picker-arrow {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.6);
				}
			}
		}
	}
}

.action-section {
	margin: 48rpx 0;
}

.result-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.compatibility-analysis {
		.overall-compatibility {
			display: flex;
			align-items: center;
			margin-bottom: 32rpx;
			padding: 32rpx;
			background: linear-gradient(135deg, #FF6B9D 0%, #E91E63 100%);
			border-radius: 16rpx;
			
			.compatibility-score {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 32rpx;
				
				.score-circle {
					width: 120rpx;
					height: 120rpx;
					border: 4rpx solid rgba(255, 255, 255, 0.3);
					border-radius: 50%;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin-bottom: 12rpx;
					
					.score-text {
						font-size: 36rpx;
						font-weight: bold;
						color: white;
					}
					
					.score-unit {
						font-size: 20rpx;
						color: rgba(255, 255, 255, 0.8);
					}
				}
				
				.score-level {
					font-size: 24rpx;
					color: white;
					font-weight: 500;
				}
			}
			
			.compatibility-desc {
				flex: 1;
				
				.desc-text {
					font-size: 28rpx;
					color: white;
					line-height: 1.6;
				}
			}
		}
		
		.compatibility-details {
			display: flex;
			flex-direction: column;
			gap: 16rpx;
			
			.detail-item {
				padding: 24rpx;
				background-color: #f8f9fa;
				border-radius: 12rpx;
				
				.detail-aspect {
					display: block;
					font-size: 28rpx;
					font-weight: 500;
					color: #000000;
					margin-bottom: 12rpx;
				}
				
				.detail-content {
					display: flex;
					justify-content: space-between;
					align-items: center;
					
					.detail-desc {
						flex: 1;
						font-size: 26rpx;
						color: rgba(0, 0, 0, 0.8);
						margin-right: 16rpx;
						line-height: 1.5;
					}
					
					.detail-score {
						.score-text {
							font-size: 24rpx;
							color: #FF6B9D;
							font-weight: 500;
						}
					}
				}
			}
		}
	}
}

.suggestions-section,
.lucky-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.suggestions-list {
		.suggestion-item {
			display: flex;
			align-items: flex-start;
			margin-bottom: 20rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.suggestion-icon {
				margin-right: 16rpx;
				margin-top: 4rpx;
				
				.icon-text {
					font-size: 32rpx;
				}
			}
			
			.suggestion-text {
				flex: 1;
				font-size: 28rpx;
				color: #000000;
				line-height: 1.6;
			}
		}
	}
	
	.lucky-elements {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 16rpx;
		
		.element-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 24rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			.element-label {
				font-size: 24rpx;
				color: rgba(0, 0, 0, 0.7);
				margin-bottom: 8rpx;
			}
			
			.element-value {
				font-size: 28rpx;
				font-weight: 500;
				color: #FF6B9D;
			}
		}
	}
}
</style>